package com.tj.view.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.LinearLayout;

import static android.content.ContentValues.TAG;

/**
 * Created by Administrator on 2017/1/13 0013.
 *
 * 优惠卷的展示: 思路是可以发现在View的上下边缘为圆心，画出的白色圆形即可。
 *
 * http://blog.csdn.net/qq_30379689/article/details/52356145
 *
 * 遇到的问题：在小米5的手机上，当radius，gap=8时，展示时优惠劵的后面有没有产生效果的部分
 * 分析原因：int roundNum = getWidth()/(radius*2 + gap*2);这段代码没有除尽，留有余数导致
 * 解决的办法: 调整radius，和gap
 *
 * 这样的view，可以简单的用叫美工来一张切图代替
 */

public class MyCardView extends LinearLayout{

    private int radius = 10;
    private int gap = 10;
    private Paint mPaint;

    public MyCardView(Context context) {
        super(context);
        init();
    }

    public MyCardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyCardView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        mPaint = new Paint();
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setDither(true);//防抖
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int roundNum = getWidth()/(radius*2 + gap*2);
        for (int i=0;i<roundNum;i++){
            //绘上面圆,形成上面的形状
            canvas.drawCircle((gap+radius)*(2*i-1),0,radius,mPaint);
            //绘下面圆
            canvas.drawCircle((gap+radius)*(2*i-1),getHeight(),radius,mPaint);
        }
        Log.d(TAG, "onDraw: getWidth="+getWidth()+",getpaddingleft="+getPaddingLeft()+",roundNum="+roundNum);
    }
}
